import 'package:finance_app/src/entities/role_permission_entity.dart';

rolePermissionEntityFromJson(RolePermissionEntity data, Map<String, dynamic> json) {
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['can_edit'] != null) {
		data.canEdit = json['can_edit'] is String
				? int.tryParse(json['can_edit'])
				: json['can_edit'].toInt();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['all_permissions'] != null) {
		data.allPermissions = (json['all_permissions'] as List).map((v) => PermissionEntity().fromJson(v)).toList();
	}
	return data;
}

Map<String, dynamic> rolePermissionEntityToJson(RolePermissionEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['can_edit'] = entity.canEdit;
	data['name'] = entity.name;
	data['all_permissions'] =  entity.allPermissions?.map((v) => v.toJson())?.toList();
	return data;
}

permissionEntityFromJson(PermissionEntity data, Map<String, dynamic> json) {
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['company_id'] != null) {
		data.companyId = json['company_id'] is String
				? int.tryParse(json['company_id'])
				: json['company_id'].toInt();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['key'] != null) {
		data.key = json['key'].toString();
	}
	if (json['checked'] != null) {
		data.checked = json['checked'] is String
				? int.tryParse(json['checked'])
				: json['checked'].toInt();
	}
	return data;
}

Map<String, dynamic> permissionEntityToJson(PermissionEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['id'] = entity.id;
	data['company_id'] = entity.companyId;
	data['name'] = entity.name;
	data['key'] = entity.key;
	data['checked'] = entity.checked;
	return data;
}